package com.iflytek.sbd.demo.fanxing;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/**
 * @program: sbd
 * @description: 利用编译后泛型擦除向集合添加非泛型支持类型数据
 * @author: 陈英俊
 * @create: 2022-01-11 10:29
 */
public class Demo2 {

    /**
     * 这样做虽然很傻吊,但是测试出来了编译之后确实泛型擦出了,只保留了原始类型,我可以使用反射向集合添加非泛型数据
     * 什么是原始类型,请看build version
     *
     * @param args
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);

        list.getClass().getMethod("add", Object.class).invoke(list, "陈亮");

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));//打印出abc
        }
    }
}
